<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Group extends CI_Controller {

        function __construct() {

                // Need to run CI_Controller constructor
                parent::__construct();

                // Load the user model
                // This is needed for checking user is logged in and has permission to access the page
                $this->load->model('user');
				$this->load->model('groups');

        }
		
		public function index() {

			// Check if the user is logged in and has permission to view page
            if ( ! $this->user->is_logged()  ) { redirect('welcome/login'); }

            // Page title to send to view
            $data['page_title'] = "Manage Groups";
            // Page content 
            $data['main_content'] = "group/group_home";
			
			if ($this->groups->get_student_groups($this->session->userdata('user_id'))) {
				$data['allowMessage'] = true;
			} else {
				$data['allowMessage'] = false;
			}
			
			$student_groups = $this->groups->get_student_groups($this->session->userdata('user_id'));
			$data['student_groups'] = $student_groups;
			
			// Template Settings
			$data['content_title'] = 'Groups';
			$data['left_sidebar'] = 'my_group';
			$data['sidebar_modifier'] = 'Hide Right';
			$data['left_sidebar_title'] = 'My Group';

            // Load the welcome page for the admin section
            $this->load->view('template', $data);
        }
		
		public function join_group() {
			// Check if the user is logged in and has permission to view page
            if ( ! $this->user->is_logged()  ) { redirect('welcome/login'); }
			
			if ( ! $this->groups->student_in_group($this->session->userdata('user_id'))) {
				// Page title to send to view
				$data['page_title'] = "Join Group";
				// Page content 
				$data['main_content'] = 'group/join_group';
				//Pagg Group List
				$data['group_data'] = $this->groups->get_group_list();

				// Template Settings
				$data['content_title'] = 'Groups';
				$data['left_sidebar'] = 'my_group';
				$data['sidebar_modifier'] = 'Hide Right';
				$data['left_sidebar_title'] = 'My Group';

				// Load the welcome page for the admin section
				$this->load->view('template', $data);
			} else {
				// Page title to send to view
				$data['page_title'] = "Error creating group";
				// Page content 
				$data['main_content'] = "group/create_error";
				$data['message'] = 'You are already in a group!';

				// Template Settings
				$data['content_title'] = 'Groups';
				$data['left_sidebar'] = 'my_group';
				$data['sidebar_modifier'] = 'Hide Right';
				$data['left_sidebar_title'] = 'My Group';
					
				$this->load->view('template', $data);
			}
		}
		
		public function add_to_group() {
			// Check if the user is logged in and has permission to view page
            if ( ! $this->user->is_logged()  ) { redirect('welcome/login'); }

            $user_id = $this->session->userdata('user_id');
			$group_id = $this->uri->segment(3);
			
			if ( $this->groups->student_in_group($user_id)) {
				// Page title to send to view
				$data['page_title'] = "Error";
				// Page content 
				$data['main_content'] = "group/join_error";

				// Template Settings
				$data['content_title'] = 'Groups';
				$data['left_sidebar'] = 'my_group';
				$data['sidebar_modifier'] = 'Hide Right';
				$data['left_sidebar_title'] = 'My Group';
				
				$this->load->view('template', $data);
			} else {
				$this->groups->add_student_to_group($user_id,$group_id);
				redirect('group');
			}
		}
		
		public function leave_group() {
			// Check if the user is logged in and has permission to view page
            if ( ! $this->user->is_logged()  ) { redirect('welcome/login'); }
			
			if ($this->uri->segment(3) === 'leave') {
				$this->groups->leave_group($this->session->userdata('user_id'),$this->uri->segment(4));
				redirect('group');
			} else {
				// Page title to send to view
				$data['page_title'] = "Leave a group";
				// Page content 
				$data['main_content'] = "group/leave_group";
				
				$student_groups = $this->groups->get_student_groups($this->session->userdata('user_id'));
				$data['student_groups'] = $student_groups;

				// Template Settings
				$data['content_title'] = 'Groups';
				$data['left_sidebar'] = 'my_group';
				$data['sidebar_modifier'] = 'Hide Right';
				$data['left_sidebar_title'] = 'My Group';

				// Load the welcome page for the admin section
				$this->load->view('template', $data);
			}
		}
		
		public function create_group() {
			// Check if the user is logged in and has permission to view page
            if ( ! $this->user->is_logged()  ) { redirect('welcome/login'); }
			
			if ($this->uri->segment(3) === 'create') {
				if ($this->input->post('group_name') <> '') {
					if ( ! $this->groups->student_in_group($this->session->userdata('user_id'))) {
						if (! $this->groups->group_exists($this->input->post('group_name'))) {
							$this->groups->create_group($this->session->userdata('user_id'), $this->input->post('group_name'));
							redirect('group');
						} else {
							// Page title to send to view
							$data['page_title'] = "Error creating group";
							// Page content 
							$data['main_content'] = "group/create_error";
							$data['message'] = 'Group already exists';

							// Template Settings
							$data['content_title'] = 'Groups';
							$data['left_sidebar'] = 'my_group';
							$data['sidebar_modifier'] = 'Hide Right';
							$data['left_sidebar_title'] = 'My Group';
								
							$this->load->view('template', $data);
						}
					} else {
						// Page title to send to view
						$data['page_title'] = "Error creating group";
						// Page content 
						$data['main_content'] = "group/create_error";
						$data['message'] = 'You are already in a group!';

						// Template Settings
						$data['content_title'] = 'Groups';
						$data['left_sidebar'] = 'my_group';
						$data['sidebar_modifier'] = 'Hide Right';
						$data['left_sidebar_title'] = 'My Group';
					
						$this->load->view('template', $data);
					}
				} else {
					// Page title to send to view
					$data['page_title'] = "Error creating group";
					// Page content 
					$data['main_content'] = "group/create_error";
					$data['message'] = 'No group name entered';

					// Template Settings
					$data['content_title'] = 'Groups';
					$data['left_sidebar'] = 'my_group';
					$data['sidebar_modifier'] = 'Hide Right';
					$data['left_sidebar_title'] = 'My Group';
					
					$this->load->view('template', $data);
				}
			} else {
				if ( ! $this->groups->student_in_group($this->session->userdata('user_id'))) {
					// Page title to send to view
					$data['page_title'] = "Create a group";
					// Page content 
					$data['main_content'] = "group/create_group_form";

					// Template Settings
					$data['content_title'] = 'Groups';
					$data['left_sidebar'] = 'my_group';
					$data['sidebar_modifier'] = 'Hide Right';
					$data['left_sidebar_title'] = 'My Group';

					// Load the welcome page for the admin section
					$this->load->view('template', $data);
				} else {
					// Page title to send to view
					$data['page_title'] = "Error creating group";
					// Page content 
					$data['main_content'] = "group/create_error";
					$data['message'] = 'You are already in a group!';

					// Template Settings
					$data['content_title'] = 'Groups';
					$data['left_sidebar'] = 'my_group';
					$data['sidebar_modifier'] = 'Hide Right';
					$data['left_sidebar_title'] = 'My Group';
					
					$this->load->view('template', $data);
				}
			}
		}
		
		public function delete_group_form() {
			// Check if the user is an administrator
			if ( ! $this->user->is_logged() ) { redirect('welcome/login'); }
			if ( ! $this->user->is_administrator() ) { redirect('welcome/login'); }
			
			// Page title to send to view
            $data['page_title'] = "Administration";
            // Page content 
            $data['main_content'] = 'group/delete_group';
			//Pagg Group List
			$data['group_data'] = $this->groups->get_group_list();

			// Template Settings
			$data['content_title'] = 'Manage Groups';
			$data['left_sidebar'] = 'admin_links';
			$data['sidebar_modifier'] = 'Hide Right';
			$data['left_sidebar_title'] = 'Administration';

            // Load the welcome page for the admin section
            $this->load->view('template', $data);
		}	
		
		public function remove_group() {
			// Check if the user is an administrator
			if ( ! $this->user->is_logged() ) { redirect('welcome/login'); }
			if ( ! $this->user->is_administrator() ) { redirect('welcome/login'); }
			
			$group_id = $this->uri->segment(3);
			
			$this->groups->delete_group($group_id);
			redirect('group/delete_group_form');
		}
		
}
?>